package com.ovopark.firecar.utils

/**
 * Object ContextUtils
 *
 * @author [Trinea](http://www.trinea.cn) 2011-10-24
 */
object ObjectUtils {
    /**
     * compare two object
     *
     * @param actual
     * @param expected
     * @return
     *  * if both are null, return true
     *  * return actual.[Object.equals]
     *
     */
    @JvmStatic
    fun isEquals(actual: Any?, expected: Any?): Boolean {
        return actual === expected || if (actual == null) expected == null else actual == expected
    }

    /**
     * convert long array to Long array
     *
     * @param source
     * @return
     */
    fun transformLongArray(source: LongArray): Array<Long?> {
        val destin = arrayOfNulls<Long>(source.size)
        for (i in source.indices) {
            destin[i] = source[i]
        }
        return destin
    }

    /**
     * convert Long array to long array
     *
     * @param source
     * @return
     */
    fun transformLongArray(source: Array<Long>): LongArray {
        val destin = LongArray(source.size)
        for (i in source.indices) {
            destin[i] = source[i]
        }
        return destin
    }

    /**
     * convert int array to Integer array
     *
     * @param source
     * @return
     */
    fun transformIntArray(source: IntArray): Array<Int?> {
        val destin = arrayOfNulls<Int>(source.size)
        for (i in source.indices) {
            destin[i] = source[i]
        }
        return destin
    }

    /**
     * convert Integer array to int array
     *
     * @param source
     * @return
     */
    fun transformIntArray(source: Array<Int>): IntArray {
        val destin = IntArray(source.size)
        for (i in source.indices) {
            destin[i] = source[i]
        }
        return destin
    }

    /**
     * compare two object
     *
     * **About result**
     *  * if v1 > v2, return 1
     *  * if v1 = v2, return 0
     *  * if v1 < v2, return -1
     *
     *
     * **About rule**
     *  * if v1 is null, v2 is null, then return 0
     *  * if v1 is null, v2 is not null, then return -1
     *  * if v1 is not null, v2 is null, then return 1
     *  * return v1.[Comparable.compareTo]
     *
     *
     * @param v1
     * @param v2
     * @return
     */
    fun <V:Any> compare(v1: V?, v2: V?): Int {
        return if (v1 == null) {
            if (v2 == null) 0 else -1
        } else if (v2 == null){
            1
        } else {
            (v1 as Comparable<V?>).compareTo(v2)
        }
    }
}